package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.TrimSpaceUtil;
import com.example.demo.domain.DTO.UserFootPrintDto;
import com.example.demo.domain.UserCollectionSpotInfo;
import com.example.demo.domain.UserFootPrint;
import com.example.demo.domain.UserInfo;
import com.example.demo.service.UserInfoService;
import com.example.demo.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author z'z'w
 * @description 针对表【USER_INFO(用户表)】的数据库操作Service实现
 * @createDate 2023-05-26 08:40:43
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
        implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 查找一个用户
     *
     * @param account 用户账号
     * @return 返回用户信息
     */
    @Override
    public UserInfo selectUserInfo(String account) {
        return userInfoMapper.selectUserInfo(account);
    }


    /**
     * 检查用户是否存在
     *
     * @param userInfo 尝试注册用户信息
     * @return boolean
     */

    @Override
    public Boolean checkUser(UserInfo userInfo) {
        return selectUserInfo(userInfo.getUsername()) != null;
    }


    /**
     * 注册插入一个用户信息
     */

    @Override
    public int insertUserInfo(UserInfo userInfo) {
        return userInfoMapper.insertUserInfo(userInfo);
    }


    /**
     * 重置用户秘密
     *
     * @param userName 用户账号
     * @return
     */
    @Override
    public boolean updateUserInfoPassword(String userName, String password) {
        return userInfoMapper.updateUserInfoPassword(userName, password) == 1;
    }

    /**
     * 重置用户名
     *
     * @param userName
     * @param userId
     * @return
     */
    @Override
    public boolean updateUserInfoUserName(String userName, String userId) {
        return userInfoMapper.updateUserInfoUserName(userName, userId) == 1;
    }


    /**
     * 用户收藏返回
     */
    @Override
    public List<UserCollectionSpotInfo> selectUserCollectionInfo(String userId) {
        List<UserCollectionSpotInfo> userCollectionSpotInfos = userInfoMapper.selectUserCollectionInfo(userId);
        //封装结果集
        List<UserCollectionSpotInfo> result = new ArrayList<>();
        int flag = 0;
        for (UserCollectionSpotInfo userCollectionSpotInfo : userCollectionSpotInfos) {
            UserCollectionSpotInfo spotInfo = (UserCollectionSpotInfo) TrimSpaceUtil.replaceBlankSpace(userCollectionSpotInfo);
            flag++;
            spotInfo.setId(flag);
            //空格去空格
            result.add(spotInfo);
        }
        return result;
    }


    /**
     * 收藏景区
     *
     * @param userId 用户ID
     * @param spotId 景区ID
     * @return
     */
    @Override
    public boolean insertUserCollectionInfo(String userId, int spotId) {
        return userInfoMapper.insertUserCollectionInfo(userId, spotId) >= 1;
    }

    /**
     * 取消景点
     */
    public boolean deleteUserCollectionInfo(String userId, int spotId) {
        return userInfoMapper.deleteUserCollectionInfo(userId, spotId);
    }

    @Override
    public boolean selectSpotCollectionFlag(String userId, int spotId) {
        try {
            return userInfoMapper.selectSpotCollectionFlag(userId, spotId);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 插入足迹信息
     *
     * @return
     */
    @Override
    public boolean insertUserFootPrint(UserFootPrint userFootPrint) {
        return userInfoMapper.insertUserFootPrint(userFootPrint) >= 1;
    }

    /**
     * 检查足迹
     *
     * @return
     */
    @Override
    public boolean selectUserFootPrint(UserFootPrint userFootPrint) {
        //为空返回true可以插入，否则反之
        return userInfoMapper.selectUserFootPrint(userFootPrint) == null;
    }

    /**
     * 该用户的足迹信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserFootPrintDto> selectUserFootPrintDto(String userId) {
        return userInfoMapper.selectUserFootPrintDto(userId);
    }


}




